package com.xl.rentkeeper.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.xl.fuse.protos.viewroom.ViewOrderStatus;
import com.xl.rentkeeper.R;
import com.xl.rentkeeper.act.jiedan.ProcessOrderDetailsAct;
import com.xl.rentkeeper.adapter.OrderAdapter;
import com.xl.rentkeeper.business.OrderLogic;
import com.xl.rentkeeper.entity.Order;
import com.xl.rentkeeper.util.TimeUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2015/7/21.
 */
public class KanfangFragment extends BaseFragment implements AdapterView.OnItemClickListener, View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {
    public ListView mLVOrder;
    public OrderAdapter mAdapter;
    private Dialog mDialog;
    private List<Order> mList = new ArrayList<>();
    private SwipeRefreshLayout mSRLList;
    private TextView mTVNodata;

    @Override
    protected View initView() {
        View view = View.inflate(mActivity, R.layout.fragment_orderlist, null);
        mLVOrder = (ListView) view.findViewById(R.id.lv_order);
        mTVNodata = (TextView) view.findViewById(R.id.tv_nodata);
        //给listview设置headerview
//        View headerView = mActivity.getLayoutInflater().inflate(R.layout.order_list_tag_item, null);

//        mTVCount = (TextView) headerView.findViewById(R.id.tv_count);
        //mTVAdd.setOnClickListener(this);
       // mAdapter = new OrderAdapter(getActivity().getLayoutInflater(), mList);
        mAdapter = new OrderAdapter(mActivity,getActivity().getLayoutInflater(), mList);
//        mLVOrder.addHeaderView(headerView);
        mLVOrder.setAdapter(mAdapter);
        mLVOrder.setOnItemClickListener(this);

        mSRLList =  (SwipeRefreshLayout)view.findViewById(R.id.id_swipe_ly);
        mSRLList.setOnRefreshListener(this);
        return view;
    }


    @Override
    protected void initData() {
        reloadData();
        if(mList.size()==0){
            OrderLogic.getInstance().queryOrder(0, OrderLogic.CMD_ORDER_IS_VIEW, 0, 0);
        }
//        mLVOrder.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
       /* switch (v.getId()) {
            case R.id.tv_add:
                Intent intent = new Intent(mActivity, AddLookRoomInfoAct.class);
                startActivity(intent);
                mActivity.overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
                break;
        }*/
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
       Intent intent = new Intent(mActivity, ProcessOrderDetailsAct.class);
        final Order order = mList.get(position);
        if (position != 0 && TextUtils.isEmpty(order.tag)){
            intent.putExtra("msg", order);
            startActivity(intent);
            mActivity.overridePendingTransition(R.anim.base_slide_right_in, R.anim.base_slide_remain);
        }

    }

    // 开始自动翻页
    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onRefresh() {
        OrderLogic.getInstance().queryOrder(0, OrderLogic.CMD_ORDER_IS_VIEW, 0, 0);
    }


    public String[] monitorEvents() {
        return new String[]{OrderLogic.CMD_ORDER_IS_VIEW, PMS_LOGIC_StewardOrder,OrderLogic.CMD_PROCESSING_ORDER_STATUS_UPDATE};
    }

    @Override
    public void onEvent(String event, boolean ret,String errInfo, Object[] data) {
        if (event.equals(OrderLogic.CMD_ORDER_IS_VIEW)) {
            mSRLList.setRefreshing(false);
            if(ret){
                reloadData();
            }
        }else if(event.equals(OrderLogic.CMD_PROCESSING_ORDER_STATUS_UPDATE)){
            if(ret){
                reloadData();
            }
        }
    }

    private void reloadData() {
        List<Order> viewList = OrderLogic.getInstance().getListByStatus(ViewOrderStatus.ORDER_IS_VIEW.getValue());
        mList.clear();
        if (viewList.size() != 0) {
            List<Order> todayBeforeList = new ArrayList<>();
            List<Order> todayList = new ArrayList<>();
            List<Order> tomorrowList = new ArrayList<>();
            List<Order> tomorrowAfterList = new ArrayList<>();
            long startToday = TimeUtils.getTodayStart();
            long endToday = TimeUtils.getTodayEnd();
            long endTomorrow = endToday+TimeUtils.dayToTimeStamp();
            for (Order o:viewList){
                long viewTime = o.viewOrder.viewTime;
                if(viewTime < startToday){
                    todayBeforeList.add(o);
                }else if(viewTime>=startToday && viewTime<=endToday){
                    todayList.add(o);
                }else if(viewTime>endToday && viewTime<=endTomorrow){
                    tomorrowList.add(o);
                }else if(viewTime>endTomorrow){
                    tomorrowAfterList.add(o);
                }
            }
            List<Order> tmpList = new ArrayList<>();
            if(todayBeforeList.size()!=0){
                Order order = new Order();
                order.tag = "已超期"+todayBeforeList.size()+"个带看";
                tmpList.add(order);
                tmpList.addAll(todayBeforeList);
            }
            if(todayList.size()!=0){
                Order order = new Order();
                order.tag = "今天共"+todayList.size()+"个带看";
                tmpList.add(order);
                tmpList.addAll(todayList);
            }
            if(tomorrowList.size()!=0){
                Order order = new Order();
                order.tag = "明天共"+tomorrowList.size()+"个带看";
                tmpList.add(order);
                tmpList.addAll(tomorrowList);
            }
            if(tomorrowAfterList.size()!=0){
                Order order = new Order();
                order.tag = "两天以后共"+tomorrowAfterList.size()+"个带看";
                tmpList.add(order);
                tmpList.addAll(tomorrowAfterList);
            }
            mList.addAll(tmpList);
        }
        refreshView();
    }

    private void refreshView() {
        if(mList.size()==0){
            mTVNodata.setVisibility(View.VISIBLE);
        }else{
            mTVNodata.setVisibility(View.INVISIBLE);
        }
        mAdapter.notifyDataSetChanged();
    }
}
